
#include "util.h"

#include "esp_dht.h"

#include "espfs.h"
#include "config.h"
#include "wifi.h"

#include "mqtt.h"


#include "esp8266_server.h"

#include "time.h"

#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>

char serverAddress[] = "public.zzzfree.cn";  // server address
int port = 80;

#include <ArduinoHttpClient.h>
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

#define ADC_PIN         34

const char *dhtFilename = "/dht.txt";  // <- SD library uses 8.3 filenames

bool isDht = false;


char buff[512];
int vref = 1100;

 

void dhtRead(){  
  String v =  _dhtRead();
  Serial.println("_dhtRead done.");
  
  server.send(200, "text/html", v );
  Serial.println("dhtRead done.");  
}

void dhtReadAll(){
  if(!isDht){
    dht.setup(2, DHTesp::DHT11); 
    isDht = true;
    dhtRead();
    return;
  }
  Serial.println("readDhtAll"); 

  String all = readFile(dhtFilename);
  
  Serial.println(all); 
 
  server.send(200, "text/html", all);
}


#include "service.h"


void setup() {

  Serial.begin(115200);
  Serial.println("Start");
  
  showHeap();
  
  Serial.print(F("\nReset reason = "));
  String resetCause = ESP.getResetReason();
  Serial.println(resetCause);

 
  loadConfiguration();
 
  String thisBoard= ARDUINO_BOARD;
  Serial.println(thisBoard);
  
//  dht.setup(2, DHTesp::DHT11); 
  wifi_connect(); 

//  mqtt_setup();

//  setupServer();
//  startServer();

  // Hostname defaults to esp8266-[ChipID]
//  ArduinoOTA.setHostname(hostname);

//  ArduinoOTA.begin();
  
//  _info("ArduinoOTA READY");
  
}

int flag = -1;
void loop() {
//  ArduinoOTA.handle();
//  server.handleClient(); 

//  mqtt_loop();

//  if( flag < 0 || ( millis() > (flag + 5 * 60 * 1000)  ) ){
    _dhtRead();  
    flag = millis() ;
//  }
  Serial.println( millis() );
  delay(3000); 
 
}

 

void sendDht(String data){

//    if (WiFi.status() != WL_CONNECTED){
//      Serial.print("[sendDht] status not ready: " + String( WiFi.status() ) );
//      return;
//    }
//  
    HTTPClient http;
    WiFiClient client;

    Serial.print("sendDht begin...\n");
    //String url =  doc["dhtServer"].as<String>();
    // url += data;
    //String url = "http://public.zzzfree.cn/iot/dht.php?l=hc"; // "http://jigsaw.w3.org/HTTP/connection.html";
    
    String url = "http://www.baidu.com";

    String str_url = doc["dhtUrl"].as<String>();
    String str_query = doc["dhtQuery"].as<String>();  
    doGet( str_url + "?" + str_query + data ); 
}







String doGet(String url){  
  

  Serial.println("start doGet..");
  //url = "/";
  
  WiFiClient client;

  String str_host = doc["dhtHost"].as<String>();
  
  const char*  host = str_host.c_str(); //   "public.zzzfree.cn";
  const int  httpsPort = 80;

  /**
   * 测试是否正常连接
   */
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return "";
  }
  delay(10);

  String postRequest =(String)("GET ") + url + " HTTP/1.1\r\n" +
    "Content-Type: text/html;charset=utf-8\r\n" +
    "Host: " + host + "\r\n" +
    "User-Agent: BuildFailureDetectorESP8266\r\n" +
    "Connection: Keep Alive\r\n\r\n";
  Serial.println(postRequest);
  client.print(postRequest);  // 发送HTTP请求

  /**
   * 展示返回的所有信息
   */
  Serial.println("Response >>>>>>>>>>>>>>>>>>>>>>"); 
  String line = client.readStringUntil('\n');
  int i = 0;
  while(line.length() != 0 && i++ < 7 ){
    Serial.println(line);
    line = client.readStringUntil('\n');
  }
  Serial.println(line);
  client.stop();
  delay(3000);

  
}
 